from pymongo.results import UpdateResult
from lina.mongodb import db
from bson import ObjectId

from lina.service.BaseService import BaseService


class AttachmentService(BaseService):

    def __init__(self):
        self.table_name = 'attachment'
        super().__init__()

    def add_case_pic(self, case_id, file_to_add):
      # 添加附件图片
        caseId = ObjectId(case_id)
        table = db()[self.table_name]
        result = table.insert_one({
            "case_id": caseId,
            "file_name": file_to_add
        })

        return table.find_one({"_id": ObjectId(result.inserted_id)})

    def get_pic_by_case_id(self, case_id):
      # 查询案件的所有附件图片
        caseId = ObjectId(case_id)
        pic_list = []
        table = db()[self.table_name]
        for pic in table.find({"case_id": caseId}):
            pic_list.append(pic)
        return pic_list

    def remove(self, file_id):
      
        table = db()[self.table_name]
        objId = ObjectId(file_id)
        table.remove({"_id": objId})
